var EMDGID;
$(function() {
	//初始化BTN
	initBtnUI();
	
	//初始化窗口
	initWindowUI();
	
	//初始化数据表
	initDataGridUI();
});

//初始化BTN
function initBtnUI(){
	//委托管理
	$('#editEntrustManagerBtn').linkbutton({plain:true,iconCls:'icon-edit'});
	$('#saveEntrustManagerBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeEntrustManagerBtn').linkbutton({plain:true,iconCls:'icon-close'});
}

//初始化窗口
function initWindowUI(){
	//初始化委托管理窗口
	$('#entrustManagerWindow').window({
		title:'委托管理',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-task',
		width:700,
		height:400
	});
}

//初始化数据表
function initDataGridUI(){
	//初始化委托管理数据表格
	
	EMDGID = GETID('entrustManagerDataGrid');
	var URL = '';
	var COLUMNS = [
	               {field:'taskName', title:'任务节点名称', width:200},
	               {field:'assigneType', title:'任务委托方式', width:100, editor: {type:'combobox', options:{url:'workflowDeployTaskAjax!loadWorkflowEntrustData', valueField:'id', textField:'name', editable:false}}},
	               {field:'assigneRoles', title:'任务委托角色', width:100, editor:'text'},
	               {field:'assignees', title:'任务委托人', width:100, editor:'text'}
	              ];
	var TOOLBAR = '#entrustManagerToolBar';
	
	DG_INIT(EMDGID, URL, COLUMNS, true, false, TOOLBAR);
}



//打开委托管理窗口
function openEntrustManagerWindow(){
	var row = $('#workflowTasksDataGrid').datagrid('getSelected');
	if(ROW_IS_NOT_NULL(row)){
		var processRow = $('#workflowProcessFunMountDataGrid').datagrid('getSelected');
		var URL = 'workflowDeployTaskAjax!loadWorkflowDeployTaskEntrustData';
		var QUERY = {id: processRow.id, taskName:row.taskNodeName};
		//alert(processRow.id);
		DG_QUERY(EMDGID, URL, QUERY);
		$('#entrustManagerWindow').window('open');
	}	
}

function editEntrustManager(){
	var row = $('#entrustManagerDataGrid').datagrid('getSelected');
	if (ROW_IS_NOT_NULL(row)) {
		var rowIndex = $('#entrustManagerDataGrid').datagrid('getRowIndex', row);
		$('#entrustManagerDataGrid').datagrid('beginEdit', rowIndex);
	}
	
	var editors = $('#entrustManagerDataGrid').datagrid('getEditors', rowIndex);
	var assigneTypeEditor = editors[0];
	var assigneRolesEditor = editors[1];
	var assigneesEditor = editors[2];
	
	var assigneType = assigneTypeEditor.target.combobox('getValue');
	
	assigneTypeEditor.target.select(function(){
		alert('eeeee');
	});
	
	//如果执行方式等于ROLE
	if(assigneType == 'ROLE'){
		//绑定任务委托角色单元格单击事件
		assigneRolesEditor.target.bind('click', function(){
			//弹出角色选择窗口并且赋值
			alert('aaaaa');
		});
	//如果执行方式等于USER
	}else if(assigneType == 'USER'){
		//绑定任务委托人单元格单击事件
		assigneesEditor.target.click(function(){
			//弹出用户选择窗口并且赋值
			alert('ddddd');
		});
	//如果执行方式等于ASSIGNE
	}else if(assigneType == 'ASSIGNE'){
		//绑定任务委托人单元格单击事件
		assigneesEditor.target.bind('click', function(){
			//弹出用户选择窗口并且赋值
		});
	}else{
		
	}
	
}


//保存任务委托人信息
function saveEntrustManager(){
	var rows = $('#entrustManagerDataGrid').datagrid('getRows');
	for ( var i = 0; i < rows.length; i++) {
		$('#entrustManagerDataGrid').datagrid('endEdit', i);
	}
	
	if ($('#entrustManagerDataGrid').datagrid('getChanges').length) {
		var updated = $('#entrustManagerDataGrid').datagrid('getChanges', "updated");
		var effectRow = new Object();
		
		if (updated.length) {
			effectRow["updated"] = JSON.stringify(updated);
		}

		$.post("workflowDeployTaskAjax!save", effectRow,
				function(data) {
						$('#entrustManagerDataGrid').datagrid('reload');
						$.messager.alert("提示", "保存成功!");					
		}, "json").error(function() {
			$('#entrustManagerDataGrid').datagrid('reload');
			$.messager.alert("提示", "保存失败!");
		});
		
	}
}

function openRoleWindow(){
	
}

//关闭委托管理窗口
function closeEntrustManagerWindow(){
	$('#entrustManagerWindow').window('close');
}
